﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Zhaoxi.FactoryDeviceSystem.Models;
using Zhaoxi.FactoryDeviceSystem.Utils;

namespace Zhaoxi.FactoryDeviceSystem
{
    public partial class FrmStoreSetting : Form
    {
        public FrmStoreSetting()
        {
            InitializeComponent();
        }

        private void FrmStoreSetting_Load(object sender, EventArgs e)
        {
            if (!Directory.Exists(CommonHelper.folder))
                Directory.CreateDirectory(CommonHelper.folder);
            LoadCboInfoTypes();
            if (CommonHelper.storeSets.Count > 0)
            {
                ReloadStoreList();
            }
            txtFPosition.Text = "0";
            cboDataTypes.SelectedIndex = 0;
        }
        /// <summary>
        /// 加载信息类别下拉框
        /// </summary>
        private void LoadCboInfoTypes()
        {
             List<StoreSetInfo> infoTypes= new List<StoreSetInfo>();
            infoTypes.Add(new StoreSetInfo() { ParaText = "温度", ParaType = "Temperature" });
            infoTypes.Add(new StoreSetInfo() { ParaText = "压力", ParaType = "Pressure" });
            infoTypes.Add(new StoreSetInfo() { ParaText = "功率", ParaType = "Power" });
            infoTypes.Add(new StoreSetInfo() { ParaText = "速度", ParaType = "Speed" });
            cboInfoTypes.DisplayMember = "ParaText";
            cboInfoTypes.ValueMember = "ParaType";
            cboInfoTypes.DataSource = infoTypes;
            cboInfoTypes.SelectedIndex = 0;
        }

        private void ReloadStoreList()
        {
           lbStoreList.Items.Clear();
            foreach (var info in CommonHelper.storeSets)
            {
                lbStoreList.Items.Add("  " + info.ParaText + "    " + info.FirstPosition + "    " + info.ReadType);
            }
        }

        bool isSaved = false;//是否已保存
       StoreSetInfo editSet = null;//选择的配置信息
        //信息提交保存
        private void btnOk_Click(object sender, EventArgs e)
        {
            //接收信息
            string paraType=cboInfoTypes.SelectedValue.ToString();
            string paraText=cboInfoTypes.Text.ToString();
            int fPosition = txtFPosition.Text.GetInt();
            string dataType=cboDataTypes.Text.ToString();
            //刷新到存储配置列表
             if(editSet==null)
            {
                if(CommonHelper.storeSets.Find(s=>s.ParaText==paraText)==null)
                {
                    CommonHelper.storeSets.Add(new StoreSetInfo()
                    {
                        ParaText = paraText,
                        ParaType = paraType,
                        FirstPosition = fPosition,
                        ReadType = dataType
                    });
                }
                else
                {
                    MessageHelper.Error("存储配置", $"已经添加了{paraText}的配置！");
                    return;
                }
            }
             else
            {
                var setInfo = editSet;
                setInfo.FirstPosition = fPosition;
                setInfo.ReadType = dataType;
            
            }
            //写入到文件
            SaveToFile();
            InitAdd();
            isSaved = true;
        }

        private void InitAdd()
        {
            //切换为新增
            cboInfoTypes.SelectedIndex = 0;
            txtFPosition.Text = "0";
            cboDataTypes.SelectedIndex = 0;
            cboInfoTypes.Enabled = true;
            editSet = null;
        }

        private void SaveToFile()
        {
            string json = JsonConvert.SerializeObject(CommonHelper.storeSets);//序列化
            File.WriteAllText(CommonHelper.storesPath, json);//写入到json文件

            MessageHelper.Success("存储配置", "存储配置保存成功！");
            ReloadStoreList();
            CommonHelper.IsUpdateStores = true;//已更改了存储配置
        }

        //选择信息，加载-----修改状态
        private void lbStoreList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lbStoreList.SelectedItem != null)
            {
                string selItem = lbStoreList.SelectedItem.ToString();
                string[] arr = selItem.Split(' ').Where(s => s != "").ToArray();//信息项数组
                string pText = arr[0];//参数文本

                StoreSetInfo setInfo = CommonHelper.storeSets.Find(s => s.ParaText == pText);
                if (setInfo != null)
                {
                    //加载配置信息到信息栏
                    cboInfoTypes.SelectedValue = setInfo.ParaType;
                    txtFPosition.Text=setInfo.FirstPosition.ToString();
                    cboDataTypes.SelectedItem = setInfo.ReadType;
                    cboInfoTypes.Enabled = false;
                }
                editSet = setInfo;
            }
        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            isSaved = false;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (isSaved == false)
            {
                if (MessageHelper.Question("关闭提示", "当前未保存，是否确定要关闭窗口？") == DialogResult.OK)
                {
                    this.Close();
                }
                else
                {
                    return;
                }
            }
            else
                this.Close();
        }
    }
}
